GBML manual / Maths

square

value times itself - square(5) is 25. Numbers bigger than about 724 go wrong, because their square no longer fits in a real.

Sega Genesis: not built yet

On other consoles this differs: see Side by side and How the consoles differ.

Syntax

square(value) -> real

Arguments

NameType
valuereal

Returns

real

Description

value times itself. square(5) is 25, and square(-3) is 9 - a minus times a minus is a plus, so the answer is never negative.

Reach for it for an area, or any time you would otherwise write the same number twice (side * side). power does the same job for any whole number of times, and sqrt goes the other way.

square hands back a real, even for a whole number. If the number is counted and should stay a whole number - an area in tiles - use square_int instead; it also goes much further before it runs out.

Example

var side = 12;
var area = square(side);

area is 144.

It is more exact than *

Multiplying two decimals with * first trims each of them to the nearest 1/64, so 0.1 * 0.1 comes out as 0.0088 (Numbers explains where that comes from). square(0.1) is 0.0098, within one 1/4096 step of the true 0.01. square takes the exact product first and only then rounds it, so a small decimal does not lose most of itself on the way.

Keep the number small

A real tops out near 524,288, and a square gets there quickly. Numbers bigger than about 724 go wrong. square(724) is 524,176, which fits. square(725) should be 525,625, which does not, and the machine hands back -522,951 instead. square(1000) is -48,576. There is no error and no warning: the answer is simply wrong, the same as for any other real that grows past its edge.

If what you want is the distance between two points, do not add two squares yourself - the sum can overflow even when each square fits. dist_to squares whole numbers instead and does not have the problem.

Cost

A 64-bit multiply, which this processor does with a short routine rather than one instruction. Measured at about 90 cycles a call, against about 50 for a plain * (both counting the loop around them), so twice a plain multiply and still a tiny part of a frame (a frame is 280,896 cycles). Cheap enough for every frame.

On the GBA

Exact to one 1/4096 step, more exact than value * value: a plain real * real first cuts both numbers down to 1/64, so 0.1 * 0.1 comes out as 0.0088, where square(0.1) is 0.0098. The ceiling is real’s own: the biggest whole-number part is about 524,287, so the square of anything past 724.07 comes out wrong, silently - the answer wraps round instead of stopping at the top. square(724) is 524,176, but square(725) is -522,951 and square(1000) is -48,576. A negative value is fine (square(-3) is 9). For the distance between two points use dist_to, which squares whole pixels in an int first and is safe for far-apart points. It hands back a real, even for a whole number: for something counted, such as an area in tiles, square_int keeps the answer a whole number.

See also: powersqrtdist_toabsNumberssquare_int